package app.fugema.shop.main.order.view;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;


import com.base.common.netBeanPackage.LiveDataWrapper;
import com.base.common.utils.DensityUtil;
import com.base.common.utils.OnClickCheckedUtil;
import com.base.common.utils.UIUtils;
import com.base.common.utils.ViewUtils;
import com.base.common.view.adapter.MyGridLayoutManager;
import com.base.common.view.adapter.MyLinearLayoutManager;
import com.base.common.view.adapter.ada.GeneralRecyclerAdapter;
import com.base.common.view.base.BaseTitleActivity;
import com.base.common.viewmodel.BaseViewObserver;
import app.fugema.shop.R;
import app.fugema.shop.constants.PublicGlobal;
import app.fugema.shop.databinding.ShoppingPayResultAcBinding;
import app.fugema.shop.main.goods.adapter.GroupBookingItemMultiType;
import app.fugema.shop.main.goods.model.GoodsPageBean;
import app.fugema.shop.main.goods.vm.GoodsVM;
import app.fugema.shop.main.home.adapter.HomeGoodsItemBaseItemMultiType;

public class ShoppingPayResultActivity extends BaseTitleActivity<ShoppingPayResultAcBinding, GoodsVM>
{

    /**
     * @param context
     * @param isSuccess 是否支付成功
     * @param type      0  拼团 1 商品  2秒杀
     */
    public static void start(Context context, boolean isSuccess, int type, String orderNumbers)
    {
        Intent intent = new Intent(context, ShoppingPayResultActivity.class);
        intent.putExtra("isSuccess", isSuccess);
        intent.putExtra("type", type);
        intent.putExtra("orderNumbers", orderNumbers);
        context.startActivity(intent);
    }


    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.shopping_pay_result_ac);

    }

    private boolean isSuccess;
    private int type;   // 0商品   1 拼团  2秒杀
    private String orderNumbers;
    private GeneralRecyclerAdapter adapter;


    @Override
    protected int getStatusBarColor()
    {
        return R.color.redColor;
    }

    @Override
    protected void initView()
    {
        super.initView();
        setCloseImg(R.mipmap.ic_back_white);
        setTitle("支付成功",R.color.white);

        isSuccess = getIntent().getBooleanExtra("isSuccess", true);
        type = getIntent().getIntExtra("type", 0);
        orderNumbers = getIntent().getStringExtra("orderNumbers");
        binding.ivImageLeftClose.setOnClickListener(new OnClickCheckedUtil() {
            @Override
            public void onClicked(View view) {
                finish();
            }
        });

        if (isSuccess)
        {
            binding.rtvOrderLook.setVisibility(View.VISIBLE);
            binding.rtvReturnHomePage.setVisibility(View.VISIBLE);

            binding.rtvPayAgain.setVisibility(View.GONE);
            binding.rtvOrderDetail.setVisibility(View.GONE);

            binding.givResultImage.setImageResource(R.mipmap.ic_pay_succ);
            binding.tvPaySate.setText("支付成功");
            binding.tvPayTime.setVisibility(View.GONE);
        }
        else
        {

            binding.givResultImage.setImageResource(R.mipmap.ic_pay_fail);
            binding.tvPaySate.setText("支付失败");
            binding.tvPayTime.setVisibility(View.VISIBLE);


            binding.rtvOrderLook.setVisibility(View.GONE);
            binding.rtvReturnHomePage.setVisibility(View.GONE);

            binding.rtvPayAgain.setVisibility(View.VISIBLE);
            binding.rtvOrderDetail.setVisibility(View.VISIBLE);
        }


        binding.rtvOrderLook.setOnClickListener(new OnClickCheckedUtil() {
            @Override
            public void onClicked(View view)
            {
                OrderDetailActivity.start(getActivity(), Long.parseLong( orderNumbers ) );
            }
        });
        binding.rtvReturnHomePage.setOnClickListener(new OnClickCheckedUtil() {
            @Override
            public void onClicked(View view) {
                PublicGlobal.getToHomePage(-1);
            }
        });

        binding.rtvPayAgain.setOnClickListener(new OnClickCheckedUtil() {
            @Override
            public void onClicked(View view) {

            }
        });
        binding.rtvOrderDetail.setOnClickListener(new OnClickCheckedUtil() {
            @Override
            public void onClicked(View view) {

            }
        });


        //拼团
        if (type == 1) {
            binding.rtvTextView.getDelegate().setBackgroundColor(0xffffffff);
            binding.rvRecyclerView.setLayoutManager(new MyLinearLayoutManager(this));
            ViewUtils.setViewMarginStart(binding.rvRecyclerView, DensityUtil.getDimens(R.dimen.dp_15), DensityUtil.getDimens(R.dimen.dp_15));
        } else {
            binding.rvRecyclerView.setLayoutManager(new MyGridLayoutManager(this, 2));
            binding.rtvTextView.getDelegate().setBackgroundColor(0xfff9f9f9);
            binding.rtvTextView.getDelegate().setCornerRadius_TL(0);
            binding.rtvTextView.getDelegate().setCornerRadius_TR(0);
            ViewUtils.setViewMarginStart(binding.rtvTextView, 0, 0);
        }


        adapter = new GeneralRecyclerAdapter() {
            @Override
            public void initMultiItemType() {
                //拼团
                if (type == 1) {
                    putMultiItemType(new GroupBookingItemMultiType());
                } else {

                }
            }
        };

        binding.rvRecyclerView.setAdapter(adapter);
    }


    @Override
    protected void initData(Intent intent)
    {
        super.initData(intent);
        // 查商品
        viewModel.getHomeGoodsByPage(1).observe(this, new BaseViewObserver<LiveDataWrapper<GoodsPageBean>>(this) {
            @Override
            protected void onSuccess(LiveDataWrapper<GoodsPageBean> data) {
                adapter.setDataList(data.data.getProduct());

            }

            @Override
            public boolean isEmptyData(LiveDataWrapper<GoodsPageBean> data) {
                return UIUtils.isEmpty(data.data.getProduct());
            }

            @Override
            protected void onEmptyLayout() {
                super.onEmptyLayout();
                binding.rtvTextView.setVisibility(View.GONE);
                binding.rvRecyclerView.setVisibility(View.GONE);
            }
        });

    }


}
